using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GP2D3D_Huiswerk2_RickBeijer_S1012261
{
    public class ProjectileList : DrawableGameComponent, IProjectileList
    {
        private List<Projectile> projectiles;
        private List<Explosion> explosions;
        private SpriteBatch spriteBatch;
        private Texture2D projectileImage;
        private Texture2D explosionImage;

        public ProjectileList(Game game, SpriteBatch spriteBatch)
            : base (game)
        {
            this.projectiles = new List<Projectile>();
            this.explosions = new List<Explosion>();
            this.spriteBatch = spriteBatch;
            this.projectileImage = game.Content.Load<Texture2D>("CannonBall Animated");
            this.explosionImage = game.Content.Load<Texture2D>("Explosion Animated");
        }

        public void AddProjectile(Point location, Vector3 velocity)
        {
            Projectile projectile = new Projectile( Game,
                                                    location,
                                                    projectileImage,
                                                    velocity );
            projectiles.Add( projectile );
            Game.Components.Add( projectile );
        }

        public void RemoveProjectile(Projectile projectile)
        {
            AddExplosion( projectile.Location );

            projectiles.Remove( projectile );
            Game.Components.Remove( projectile );
        }

        public void AddExplosion(Point location)
        {
            Explosion explosion = new Explosion( Game,
                                                 location,
                                                 explosionImage );
            explosions.Add( explosion );
            Game.Components.Add( explosion );
        }

        public void RemoveExplosion(Explosion explosion)
        {
            explosions.Remove( explosion );
            Game.Components.Remove( explosion );
        }

        public override void Draw(GameTime gameTime)
        {
            // Draw all projectiles and explosions using the same SpriteBatch. This is more efficient.
            spriteBatch.Begin();
            foreach ( Projectile projectile in projectiles )
            {
                spriteBatch.Draw( projectile.Image,
                                  new Rectangle( projectile.Location.X,
                                                 projectile.Location.Y,
                                                 projectile.CurrentFrame.Width,
                                                 projectile.CurrentFrame.Height ),
                                  projectile.CurrentFrame,
                                  Color.White,
                                  0,
                                  projectile.CurrentOrigin,
                                  SpriteEffects.None,
                                  1 );
            }

            foreach ( Explosion explosion in explosions )
            {
                spriteBatch.Draw( explosion.Image,
                                  new Rectangle( explosion.Location.X,
                                                 explosion.Location.Y,
                                                 explosion.CurrentFrame.Width,
                                                 explosion.CurrentFrame.Height ),
                                  explosion.CurrentFrame,
                                  Color.White,
                                  MathHelper.ToRadians(270),
                                  explosion.CurrentOrigin,
                                  SpriteEffects.None,
                                  1 );
            }
            spriteBatch.End();

            base.Draw( gameTime );
        }
    }
}